<?php
namespace app\mini\controller\v1\client;

use app\common\library\MemberVo;
use app\common\logic\MemberLogic;
use app\common\model\Config;
use app\common\model\Coupon;
use app\common\model\Member;
use app\common\model\ServiceTimes;
use app\mini\controller\Common;
use think\Db;
use think\Env;
use think\Request;
use think\response\Json;

class Center extends Common
{

    public $MemberLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->MemberLogic = new MemberLogic();
    }

    /**
     * 个人中心(我的)
     * @return Json
     */
    public function index(): Json
    {
        $res = $this->exception(function () {
            $service_num = Config::where(['name'=>'service_num'])->value('value');
            $member_info = Member::get(['id'=>$this->member_info_arr['member_id']]);
            $data['coupons'] = Coupon::getCouponNumByMemberId($this->member_info_arr['member_id']);//获取未使用的优惠券数量
            $data['balance'] = sprintf("%.2f",$member_info['balance']);//余额
            $data['member_name'] = $member_info['member_name'] ?? '';//昵称
            $data['member_avatar'] = $member_info['member_avatar'] ?? '';//头像
            $data['member_mobile'] = empty($member_info['member_mobile']) ? $member_info['member_name'] : $member_info['member_mobile'];
            $data['is_vip'] = $member_info['is_vip'];//是否是vip 0不是 1是
            $data['service_times'] = 0;//服务次数
            $data['surplus_times'] = 0;//剩余次数
            $data['service_num'] = intval($service_num);//系统设置的达标次数
            $days = intval(Env::get('app.days',180));
            $days = $days-1;
            $data['service_times'] = \app\common\model\Order::whereBetween('audit_time',[strtotime(date('Y-m-d',strtotime("-{$days} days"))),time()])->where(['member_id'=>MemberVo::instance()->getMemberId()])->whereIn('order_status',[3,4])->count('id');//当前时间往前推180的总完成数
//            $info = ServiceTimes::getInfoByMemberId($this->member_info_arr['member_id']);
//            $data['service_times'] = $info['times'];//服务次数
            if ($member_info['is_vip'] == 0){ //vip
                $data['surplus_times'] = bcsub($service_num,$data['service_times'],0);//剩余次数
                if ($data['surplus_times'] < 0)
                    $data['surplus_times'] = 0;
            }
            $data['wait_audit'] = \app\common\model\Order::where(['member_id'=>MemberVo::instance()->getMemberId(),'order_status'=>\app\common\model\Order::ORDER_STATUS_WAIT_AUDIT])->count('id');
            $data['balance_show_drop'] = $member_info['balance_show_drop'];
            $data['phoneList'] = ['15959397929'];
            return $data;
        });
        return $this->response($res);
    }

    /**
     * 我的权权益
     * @return Json
     */
    public function permission(): Json
    {
        $res = $this->exception(function () {
            return $this->MemberLogic->permission();
        });
        return $this->response($res);
    }

    /**
     * 关于我们列表
     * @return array
     */
    public function about_list(): Json
    {
        $res = $this->exception(function () {
            return [
                ['name'=>'platform_introduction','title'=>'平台介绍'],
                ['name'=>'business_license','title'=>'营业执照'],
                ['name'=>'service_agreement','title'=>'用户服务协议'],
                ['name'=>'privacy_policy','title'=>'隐私政策'],
                ['name'=>'description_responsibility','title'=>'责任说明']
            ];
        });
        return $this->response($res);
    }

    /**
     * 关于我们
     * @return array
     */
    public function about(): Json
    {
        $res = $this->exception(function () {
            $content = Db::name('config')->where('name',input('name'))->value('value');
            return $content;
        });
        return $this->response($res);
    }


}